declare boundary-space preserve;

(:
Part 2 / Q6
The SIDs of pairs of students whose answers on the quiz were exactly the same.
In other words, the set of questions they gave answers to is the same,
and the answers are the same also.
:)

(: Returns true if students $s1 and $s2 gave the exact same answers. :)
declare function local:same-answers($s1 as element(Student),
    $s2 as element(Student)) {
    count(
        for $r1 in $s1/QuestionResponse
        where some $r2 in $s2/QuestionResponse satisfies
            ($r1/@QID eq $r2/@QID) and ($r1/@Response eq $r2/@Response)
        return $r1
    ) eq count($s1/QuestionResponse)
};

let $students := fn:doc("class.xml")/ClassResponses/Student
for $s1 in $students
return (for $s2 in $students
        where $s1/@sid lt $s2/@sid and local:same-answers($s1, $s2)
        return <Pair sid1="{$s1/@sid}" sid2="{$s2/@sid}"/>)
